<template>
    <SysDialog :title="dialogBasic.title" :width="dialogBasic.width" :height="dialogBasic.height"
        :show-footer="dialogBasic.showFooter" :visible="dialogBasic.visible" :margin-top="dialogBasic.marginTop"
        @on-close="onClose" @on-confirm="onConfirm">
        <template v-slot:content>
            <div class="channel-form">
                <el-form ref="ruleFormRef" :model="channelForm" :rules="rules" label-width="210px" class="demo-ruleForm"
                    status-icon>
                    <el-form-item label="拦截回调" prop="0.value">
                        <!-- <el-input v-model="channelForm[0].value" placeholder="请选择拦截回调" /> -->
                        <el-radio-group v-model="channelForm[0].value">
                            <el-radio value="1" size="large">是</el-radio>
                            <el-radio value="2" size="large">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="当前渠道总请求数">
                        {{ showTotal && showTotal[0].value }}
                    </el-form-item>
                    <el-form-item prop="1.value">
                        <template #label>
                            <el-tooltip placement="top">
                                <template #content>
                                    所有请求都会增加请求数，当请求数量达到该数量时，会报出：已超订单数量限制
                                </template>
                                <el-button link>该渠道的总订单数 &nbsp;<el-icon color="red">
                                        <Reading />
                                    </el-icon></el-button>
                            </el-tooltip>
                        </template>
                        <el-input-number v-model.number="channelForm[1].value" clearable :min="1" style="width: 100%;"
                            :max="999999" placeholder="请输入允许该渠道的总订单数1-999999" :controls="false" />
                    </el-form-item>
                    <el-form-item label="每小时提交签约订单上线？" prop="2.value">
                        <template #label>
                            <el-tooltip placement="top">
                                <template #content>
                                    1.所有请求都会增加提单数量，当数量达到该数值时，会报出：当前已超每小时订单数量限制<br />
                                    2.如需临时增加该数量 <br />
                                    &nbsp;&nbsp; 如之前为 10 ,无需修改提交一次即可为当前小时再次增加 10 次调用<br />
                                    &nbsp;&nbsp; 如之前为 10 ,修改为 5提交一次即可为当前小时再次增加 5 次调用<br />
                                    3.将其值改大或改小下一个小时才会生效
                                </template>
                                <el-button link>每小时提交签约订单上限 &nbsp;<el-icon color="red">
                                        <Reading />
                                    </el-icon></el-button>
                            </el-tooltip>
                        </template>
                        <el-input-number v-model.number="channelForm[2].value" clearable :min="1" style="width: 100%;"
                            :max="999999" placeholder="请输入每小时提交签约订单上限1-999999" :controls="false" />
                    </el-form-item>
                    <el-form-item label="每日固定成功单数" prop="3.value">
                        <el-input-number v-model.number="channelForm[3].value" clearable :min="1" style="width: 100%;"
                            :max="999999" placeholder="请输入每日固定成功单数1-999999" :controls="false" />
                    </el-form-item>
                    <el-form-item label="激活拦截比例" prop="4.value">

                        <el-input-number v-model.number="channelForm[4].value" clearable :min="0" style="width: 100%;"
                            :max="100" placeholder="请输入每日固定成功单数0-100" :controls="false" />
                    </el-form-item>
                    <el-form-item label="极限转化率%" prop="5.value">

                        <el-input-number v-model.number="channelForm[5].value" clearable :min="0" style="width: 100%;"
                            :max="100" placeholder="请输入极限转化率0-100" :controls="false" />
                    </el-form-item>
                    <el-form-item label="极限解约率%" prop="6.value">

                        <el-input-number v-model.number="channelForm[6].value" clearable :min="0" style="width: 100%;"
                            :max="100" placeholder="请输入极限解约率0-100" :controls="false" />
                    </el-form-item>
                    <el-form-item label="极限核销率%" prop="7.value">

                        <el-input-number v-model.number="channelForm[7].value" clearable :min="0" style="width: 100%;"
                            :max="100" placeholder="请输入极限解约率0-100" :controls="false" />
                    </el-form-item>

                </el-form>
            </div>
        </template>
    </SysDialog>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { Search, Minus, Plus } from "@element-plus/icons-vue";
import { useEnumStore } from "@/store/enumeration";
import { storeToRefs } from "pinia";
import { ElMessage } from "element-plus";
import { additionalSavePost } from "@/api/userMangement";
import useInstance from "@/hooks/useInstance";
const { blackAndWhiteType } = storeToRefs(useEnumStore())
defineOptions({
    name: "addListWhite",
});

const emit = defineEmits(["refresh"]);
const { global } = useInstance();
const dialogBasic = ref({
    title: "渠道附加信息管理",
    visible: false,
    width: 600,
    height: 550,
    showFooter: true,
    marginTop: "10vh",
});

const ruleFormRef = ref<FormInstance>();
/* 

*/
const channelForm = ref<any>([
    {
        channelCode: null, //  渠道编码
        code: 'EnableOrderInterception', //  附加信息编码 拦截回调
        value: null, // 附加信息值
        remarks: null, //备注
    },
    {
        channelCode: null, //  渠道编码
        code: 'TotalOrderQuantityLimit', //  附加信息编码 允许该渠道的总订单数
        value: null, // 附加信息值
        remarks: null, //备注
    },
    {
        channelCode: null, //  渠道编码
        code: 'HourOrderQuantityLimit', //  附加信息编码
        value: null, // 附加信息值
        remarks: null, //备注
    },
    {
        channelCode: null, //  渠道编码
        code: 'DailyOrderQuantitySuccess', //  每日固定成功单数
        value: 50, // 附加信息值
        remarks: null, //备注
    },
    {
        channelCode: null, //  渠道编码
        code: 'OrderInterceptionRate', //  附加信息编码
        value: null, // 附加信息值
        remarks: null, //备注
    },
    {
        channelCode: null, //  渠道编码
        code: 'MaxConversionRate', //  极限转化率
        value: null, // 附加信息值
        remarks: null, //备注
    },
    {
        channelCode: null, //  渠道编码
        code: 'MaxRescindRate', //  极限解约率
        value: null, // 附加信息值
        remarks: null, //备注
    },
    {
        channelCode: null, //  渠道编码
        code: 'MaxRedeemRate', //  极限核销率
        value: null, // 附加信息值
        remarks: null, //备注
    }

]);
const rules = reactive<FormRules>({

    '0.value': [
        {
            required: true,
            trigger: "blur",
            message: "请选择拦截回调",
        },
    ],
    '1.value': [
        {
            required: true,
            trigger: "blur",
            message: "请输入允许该渠道的总订单数",
        },
    ],

    '2.value': [
        {
            required: true,
            trigger: "blur",
            message: "请输入每小时提交签约订单上线",
        },
    ],
    '3.value': [
        {
            required: true,
            trigger: "blur",
            message: "请输入每日固定成功单数",
        },
    ],
    '4.value': [
        {
            required: true,
            trigger: "blur",
            message: "请输入激活拦截比例",
        },
    ],
    '5.value': [
        {
            required: true,
            trigger: "blur",
            message: "请输入极限转化率",
        },
    ],
    '6.value': [
        {
            required: true,
            trigger: "blur",
            message: "请输入极限解约率",
        },
    ],
    '7.value': [
        {
            required: true,
            trigger: "blur",
            message: "请输入极限核销率",
        },
    ],

});
const showTotal = ref<any>([])
//显示弹框
const show = (type: string, row?: any, data?: any) => {
    showTotal.value = []
    dialogBasic.value.visible = true;
    // Object.assign(channelForm.value, row)
    if (data && data.length > 1) {
        data.forEach((item: any) => {
            if (!item.channelCode) item.channelCode = row.channelCode
            if (item.code == 'CurrentOrderRequestQuantity') {
                showTotal.value.push(item)
            }
            // 怎么处理
            channelForm.value.forEach((it: any, index: number) => {
                // 这个地方做一件什么事情
                if (item.code == it.code) {
                    channelForm.value[index] = item
                }
            })
        })

        Object.keys(channelForm.value).forEach((item: any) => {
            // 这个地方就是 看有没得 code
            if (!channelForm.value[item].channelCode) {
                console.log('没有数据');
                
                channelForm.value[item].channelCode = row.channelCode
                channelForm.value[item].remarks = null
                channelForm.value[item].id = null
                // 给默认值
                if (channelForm.value[item].code == 'DailyOrderQuantitySuccess') {
                    channelForm.value[item].value = 50
                } else if (channelForm.value[item].code == 'MaxConversionRate') {
                    channelForm.value[item].value = 20
                } else if (channelForm.value[item].code == 'MaxRescindRate') {
                    channelForm.value[item].value = 50
                } else if (channelForm.value[item].code == 'MaxRedeemRate') {
                    channelForm.value[item].value = 50
                } else {
                    channelForm.value[item].value = null
                }
            }

        })
    } else if (data.length <= 1) {
        data && data.forEach((item: any) => {
            if (!item.channelCode) item.channelCode = row.channelCode
            if (item.code == 'CurrentOrderRequestQuantity') {
                showTotal.value.push(item)
            }
        })

        Object.keys(channelForm.value).forEach((item: any) => {
            channelForm.value[item].channelCode = row.channelCode
            channelForm.value[item].remarks = null
            channelForm.value[item].id = null
            // 给默认值
            if (channelForm.value[item].code == 'DailyOrderQuantitySuccess') {
                channelForm.value[item].value = 50
            } else if (channelForm.value[item].code == 'MaxConversionRate') {
                channelForm.value[item].value = 20
            } else if (channelForm.value[item].code == 'MaxRescindRate') {
                channelForm.value[item].value = 50
            } else if (channelForm.value[item].code == 'MaxRedeemRate') {
                channelForm.value[item].value = 50
            } else {
                channelForm.value[item].value = null
            }
        })
    }


};
const onClose = () => {
    dialogBasic.value.visible = false;
    Object.keys(channelForm.value).forEach((item: any) => {
        channelForm.value[item].channelCode = null
        channelForm.value[item].value = null
        channelForm.value[item].remarks = null
        channelForm.value[item].id = null
    })
    resetForm();
};
const onConfirm = async () => {
    await ruleFormRef.value?.validate(async (isOk) => {
        if (isOk) {
            const res = await additionalSavePost(channelForm.value);
            if (res.code == 20000) {
                ElMessage({
                    type: "success",
                    message: "渠道附加信息管理保存成功",
                });
            }
            emit("refresh");
            onClose();
        } else {
            ElMessage({
                type: "error",
                message: "渠道附加信息管理保存失败 请重试！",
            });
        }
    });
};

const resetForm = () => {
    ruleFormRef.value?.resetFields();
};

defineExpose({
    show,
});
</script>


<style lang="scss" scoped>
:deep(.el-input-number) .el-input__inner {
    text-align: left;
}
</style>
